package com.ctrip.ec.biz.dao.role;
import com.ctrip.ec.dal.DaoBase;
import com.ctrip.ec.biz.entity.role.RoleUserEntity;
import com.ctrip.ec.dal.DbAccess;
import com.ctrip.ec.dal.IdConvert;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

@Service
public class RoleUserDao extends DaoBase<RoleUserEntity> {
    public RoleUserDao() {
        super(RoleUserEntity.class);
    }
    public RoleUserDao(DbAccess db) {
        super(RoleUserEntity.class,db);
    }

    public List<Long> listByUserId(Long userId) throws SQLException {
        String sql = "select role_id from " + this.getTableName() + " where user_id = ?";
        return db.query(Long.class,sql,userId);
    }

    public void saveByUserId(List<Long> roleIds, Long userId) throws SQLException {
        if(roleIds!= null && roleIds.size()>0){
            String sql = "delete from " + this.getTableName() + " where user_id = ?";
            this.db.execute(sql,userId);

            for(Long rid :roleIds){
                RoleUserEntity entity = new RoleUserEntity();
                entity.setRoleId(rid);
                entity.setUserId(userId);
                this.insert(entity);
            }
        }
    }
}
